import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:part_time_job_pro/common/cache/app_box.dart';
import 'package:part_time_job_pro/common/style/theme.dart';

import 'state.dart';

class ThemeSettingLogic extends GetxController {
  final ThemeSettingState state = ThemeSettingState();

  @override
  void onInit() {
    super.onInit();
    state.currentThemeIndex.value = AppBox.shared.theme;
  }

  ///切换主题
  void changeTheme(BuildContext context, int themeIndex) {
    ThemeData themeData;
    switch (themeIndex) {
      case 0: //跟隨系統
        themeData = MediaQuery.of(context).platformBrightness == Brightness.dark
            ? AppTheme.darkTheme
            : AppTheme.lightTheme;
        break;
      case 1: //浅色模式
        themeData = AppTheme.lightTheme;
        break;
      case 2: //深色模式
        themeData = AppTheme.darkTheme;
        break;
      default:
        themeData = MediaQuery.of(context).platformBrightness == Brightness.dark
            ? AppTheme.darkTheme
            : AppTheme.lightTheme;
        break;
    }
    //保存到本地
    debugPrint('保存的主题索引为：$themeIndex');
    AppBox.shared.theme = themeIndex;
    Get.changeTheme(themeData);
    update();
    //返回
    Get.back(result: themeIndex);
  }
}
